#include "head.h"
// 用户 - 购票退票
void consumption(FlightSystem * Flight_list,U_data log_data)
{
    // 用户信息合表
    U_list *list = S_List_init();
    list = S_List_merge(list, Flight_list->ADMIN_USER);    // 管理员账户信息文件
    list = S_List_merge(list, Flight_list->REGULAR_USER);  // 普通用户信息文件
    list = S_List_merge(list, Flight_list->SILVER_USER);   // 银卡会员信息文件 (9折)
    list = S_List_merge(list, Flight_list->GOLD_USER);     // 金卡会员信息文件 (8折)
    list = S_List_merge(list, Flight_list->PLATINUM_USER); // 白金会员信息文件 (7折)
    D_list * order = R_List_init();
    R_List_File_scanf(order, "./data/order_member.txt");
    int flag = 0;
    const wchar_t *title_consumption = L"购票、退票、改签界面";
    const wchar_t *option_consumption[] = {
    L"购票",
    L"退票",
    L"改签",
    L"返回"};
    while (1)
    {
        flag = display_menu(title_consumption, option_consumption, 4);
        switch (flag)
        {
        case 1:
            buy_ticket (Flight_list, log_data, list, order); // 购票
            break;
        case 2:
            get_ticket (Flight_list, log_data, list, order); // 退票
            break;
        case 3:
            ticket_changing(Flight_list, log_data, list, order);// 改签     
            break;
        case 4:
            return ;    
        default:
            break;
        }
    }
}
// 用户 - 余额充值
void Balance_recharge(FlightSystem * Flight_list ,U_data log_data)
{
    // 用户信息合表
    U_list *list = S_List_init();
    list = S_List_merge(list, Flight_list->ADMIN_USER);    // 管理员账户信息文件
    list = S_List_merge(list, Flight_list->REGULAR_USER);  // 普通用户信息文件
    list = S_List_merge(list, Flight_list->SILVER_USER);   // 银卡会员信息文件 (9折)
    list = S_List_merge(list, Flight_list->GOLD_USER);     // 金卡会员信息文件 (8折)
    list = S_List_merge(list, Flight_list->PLATINUM_USER); // 白金会员信息文件 (7折)
    int money;
    printf("输入您要充值的金额\n");
    scanf("%d",&money);
    U_node* phph = S_List_find_name(list, log_data.name);
    phph->data.value += money;
    phph->data.accumulated += money;
    printf("充值成功\n");
    printf("您的余额为:%d\n",phph->data.value);
    sleep(2);
    if (phph->data.accumulated < 10000)
    {
        U_node* RPG =S_List_find_name(Flight_list->REGULAR_USER, log_data.name);
        RPG->data.value = phph->data.value;
        RPG->data.accumulated = phph->data.accumulated;
        S_List_File_print(Flight_list->REGULAR_USER,REGULAR_FILE);
    }
    if (phph->data.accumulated >= 10000 && phph->data.accumulated < 15000)
    {
        log_data.grade = 3;
        S_List_insert(Flight_list->SILVER_USER , log_data ,1);
        U_node* RPG_1 =S_List_find_name(Flight_list->SILVER_USER, log_data.name);
        RPG_1->data.value = phph->data.value;
        RPG_1->data.accumulated = phph->data.accumulated;
        S_List_File_print(Flight_list->SILVER_USER,SILVER_FILE);
        printf("恭喜您成为白银会员!\n");
        sleep(2);
        int D = find_user_by_name(Flight_list->REGULAR_USER, log_data.name);
        S_List_delete(Flight_list->REGULAR_USER, D);
        S_List_File_print(Flight_list->REGULAR_USER,REGULAR_FILE);
    }
    if (phph->data.accumulated >= 15000 && phph->data.accumulated < 20000)
    {
        log_data.grade = 4;
        S_List_insert(Flight_list->GOLD_USER , log_data ,1);
        U_node* RPG_2 =S_List_find_name(Flight_list->GOLD_USER, log_data.name);
        RPG_2->data.value = phph->data.value;
        RPG_2->data.accumulated = phph->data.accumulated;
        S_List_File_print(Flight_list->GOLD_USER,GOLD_FILE);
        printf("恭喜您成为黄金会员!\n");
        sleep(2);
        if (phph->data.grade == 2)
        {
            int D = find_user_by_name(Flight_list->REGULAR_USER, log_data.name);
            S_List_delete(Flight_list->REGULAR_USER, D);
            S_List_File_print(Flight_list->REGULAR_USER,REGULAR_FILE);
        }
        if (phph->data.grade == 3)
        {
            int D = find_user_by_name(Flight_list->SILVER_USER, log_data.name);
            S_List_delete(Flight_list->SILVER_USER, D);
            S_List_File_print(Flight_list->SILVER_USER,SILVER_FILE);
        }
    }
    if (phph->data.accumulated >= 20000)
    {
        log_data.grade = 4;
        S_List_insert(Flight_list->PLATINUM_USER , log_data ,1);
        U_node* RPG_3 =S_List_find_name(Flight_list->PLATINUM_USER, log_data.name);
        RPG_3->data.value = phph->data.value;
        RPG_3->data.accumulated = phph->data.accumulated;
        S_List_File_print(Flight_list->PLATINUM_USER,PLATINUM_FILE);
        printf("恭喜您成为白金会员!\n");
        sleep(2);
        if (phph->data.grade == 2)
        {
            int D = find_user_by_name(Flight_list->REGULAR_USER, log_data.name);
            S_List_delete(Flight_list->REGULAR_USER, D);
            S_List_File_print(Flight_list->REGULAR_USER,REGULAR_FILE);
        }
        if (phph->data.grade == 3)
        {
            int D = find_user_by_name(Flight_list->SILVER_USER, log_data.name);
            S_List_delete(Flight_list->SILVER_USER, D);
            S_List_File_print(Flight_list->SILVER_USER,SILVER_FILE);
        }
        if (phph->data.grade == 4)
        {
            int D = find_user_by_name(Flight_list->GOLD_USER, log_data.name);
            S_List_delete(Flight_list->GOLD_USER, D);
            S_List_File_print(Flight_list->GOLD_USER,GOLD_FILE);
        }
    }
    return ;
}
// 用户 - 查看航班
void view_flight(F_list* flight)
{
    char gu = 'A';
    D_List_print(flight);
    sleep(1);
    while (1)
    {
        printf("查看完成请输入'Y'\n");
        scanf("%c", &gu);
        if (gu == 'Y') 
        {
            return ;
        }
        else printf("请正确输入\n");
    }
}
// 用户 - 修改密码
void change_user_password(FlightSystem * Flight ,U_data log_data)
{
    char password[128];
    while (1)
    {
    printf("请输入新的密码:\n");
    scanf("%s" , log_data.passwd);
    printf("请再次输入新的密码:\n");
    scanf("%s" , password);
    if (strcmp(password , log_data.passwd))
        {
            printf("两次密码输入不相同 请重新输入\n");
        }
    else break;
    }
    U_node* RPG_1;
    U_node* RPG_2;
    U_node* RPG_3;
    U_node* RPG_4;
    switch (log_data.grade)
    {
    case 2:
        RPG_1 =S_List_find_name(Flight->REGULAR_USER, log_data.name);
        strcpy(RPG_1->data.passwd, log_data.passwd );
        S_List_File_print(Flight->REGULAR_USER, REGULAR_FILE);
        break;
    case 3:
        RPG_2 =S_List_find_name(Flight->SILVER_USER, log_data.name);
        strcpy(RPG_2->data.passwd, log_data.passwd );
        S_List_File_print(Flight->SILVER_USER, SILVER_FILE);
        break;
    case 4:
        RPG_3 =S_List_find_name(Flight->GOLD_USER, log_data.name);
        strcpy(RPG_3->data.passwd, log_data.passwd );
        S_List_File_print(Flight->GOLD_USER, GOLD_FILE);
        break;
    case 5:
        RPG_4 =S_List_find_name(Flight->PLATINUM_USER, log_data.name);
        strcpy(RPG_4->data.passwd, log_data.passwd );
        S_List_File_print(Flight->PLATINUM_USER, PLATINUM_FILE);
        break;
    default:
        break;
    }
    printf("修改密码成功\n");
    sleep(2);
    return ;
}
// 用户 - 我的订单
void MY_order(U_data log_data)
{
    D_list * order = R_List_init();
    R_List_File_scanf(order, "./data/order_member.txt");
    printf("以下是您的订单\n");
    int L = findOrder(order, log_data.name);
    if (L == 0)
    {
        printf("您还未下单哟\n");
        sleep(2);
        return ;
    }
    char kck;
    printf("请输入'Y'继续\n");
    while (1)
    {
        scanf("%c", &kck);
        if (kck == 'Y')
        {
            return ;
        } 
    }
}